#include "imageinfo.h"
#include <QDebug>
QDir ImageInfo::getDir() const {
  return dir;
}

void ImageInfo::setDir(const QDir & value) {
  dir = value;
}

QTreeWidgetItem *ImageInfo::getTreeItem() {
  QStringList data = {uid, referencedFileID, getAbsolutePath()};
  return new QTreeWidgetItem(data);
}

ImageInfo::ImageInfo() {

}

QString ImageInfo::getReferencedFileID() const {
  return referencedFileID;
}

void ImageInfo::setReferencedFileID(const QString & value) {
  referencedFileID = value;
}

QString ImageInfo::getUid() const {
  return uid;
}

void ImageInfo::setUid(const QString & value) {
  uid = value;
}

QString ImageInfo::getAbsolutePath() {
  qDebug() << "FILEID: " << referencedFileID;
  qDebug() << "DIR: " << dir.absolutePath();
  QStringList path = referencedFileID.split("\\");
  if( path.isEmpty() )
    path = referencedFileID.split("/");
  QFileInfo fileInfo;
  QFile imagePath;
  if(!path.empty()) {
    foreach (QString f, path) {
      f = f.trimmed();
      fileInfo.setFile( dir, f);
      if(!fileInfo.exists()) {
        f = f.toLower();
        fileInfo.setFile( dir, f);
        if(!fileInfo.exists()) {
          break;
        }
      }
      qDebug() << "ABSOLUTE" << fileInfo.absoluteFilePath();
      if(fileInfo.isDir()) {
        if( !dir.cd(f) ) {
          break;
        }
      } else if(fileInfo.isFile()) {
        imagePath.setFileName(dir.absoluteFilePath(f));
        qDebug() << "isFile! : " << imagePath.fileName();
        break;
      }
    }
  }
  qDebug() << "IMAGEPATH: " << imagePath.fileName();
  return imagePath.fileName();
}
